
package com.x52im.rbchat.logic.chat_friend.vv;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.util.Log;

import com.vc.core.VcController;

/**
 * 实时语音聊天核心提供者实现类.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public class VoiceAloneProvider
{
	private final static String TAG = VoiceAloneProvider.class.getSimpleName();
	
	/**
	 * 监听耳机插孔
	 */
	private BroadcastsHandler mBoardcaseHandler = null;
	/**
	 * 视频通话控制器
	 */
	protected VcController myController;
	/**
	 * 全局声音管理器，此处主要用来做其他声音的处理，例如电话，音乐等
	 */
	protected AudioManager mAudioManager = null;
//	protected boolean isMute = false;
	
	/** 父Activity的句柄 */
	protected Activity parentActivity = null;
	
	public VoiceAloneProvider(Activity parentActivity)
	{
		this.parentActivity = parentActivity;
	}
	
	protected void init()
	{
		//全局声音控制器
		mAudioManager = (AudioManager)(parentActivity.getSystemService(Context.AUDIO_SERVICE));
		
		//获取控制器单例
		myController = VVP2PProvider.getInstance(parentActivity).getVcController();//VcController.getInstance();
		//初始化控制器
		myController.init(parentActivity.getApplicationContext());
		
		//监听耳机插孔
        mBoardcaseHandler = new BroadcastsHandler();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_HEADSET_PLUG);
		parentActivity.registerReceiver(mBoardcaseHandler, filter);
	}
	
	/**
	 * @see com.vc.activity.VideoActivity#releaseMobileIMSDK()
	 */
	protected void release()
	{
		Log.i(TAG, "RealTimeVoiceProvider.release begin...");
    	
		if(mBoardcaseHandler != null)
		{
			parentActivity.unregisterReceiver(mBoardcaseHandler);
			mBoardcaseHandler = null;
		}
		
		if(myController!=null)
		{
			myController.uninit();
		}
		
		//打开外放
		if(mAudioManager!=null)
		{
			mAudioManager.setSpeakerphoneOn(true);			
			mAudioManager = null;
		}
		
		Log.i(TAG, "RealTimeVoiceProvider.release end...");
	}
	
    /**
     * 声音播放，语音开始
     * 
     * @see com.vc.demo.activity.VideoChatActivity#audioStart()
     */
	protected boolean audioStart(){
		boolean ret = false;
		if(myController != null) {
			ret = myController.initAudio(parentActivity.getApplicationContext(),AudioManager.MODE_NORMAL);
			if(ret){
				myController.startAudio();
			}
		}else{
			Log.e(TAG,"audioStart fail,myController is null");
		}
		return ret;
	}
	
	
	/**
	 * @see com.vc.activity.VideoActivity.BroadcastsHandler
	 */
	private class BroadcastsHandler extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equalsIgnoreCase(Intent.ACTION_HEADSET_PLUG)) {
				//有耳机插入
				if (intent.getIntExtra("state", 0) == 1) {
					Log.d(TAG, "broadcasts speakerphone off");
					mAudioManager.setSpeakerphoneOn(false);//外放关闭
				} else {//无耳机插入，外放打开
					Log.d(TAG, "broadcasts speakerphone on");
					mAudioManager.setSpeakerphoneOn(true);//外放打开
				}
			}
		}
	}
}
